package edu.gatech.oops.servlets;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;

/**
 * Servlet implementation class registerCourse
 */
public class registerCourse extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public registerCourse() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	String studentName = request.getParameter("studentName");
    	String CourseName = request.getParameter("courseName");
  try{
    	File file = new File("/home/pushkara/workspace/LabAssistan/" + CourseName + ".txt");
    	BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
    	writer.write(studentName + "\n");
    	writer.close();
  }
  catch(FileNotFoundException e){
  File eFile = new File("/home/pushkara/workspace/LabAssistan/LogExceptions.txt");
            BufferedWriter eWriter = new BufferedWriter(new FileWriter(eFile, true));
            eWriter.write(Calendar.getInstance().getTime() + ": Couldn't register for class.\n");
            eWriter.close();
  }
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

}
